package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.res.CartRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.res.ResponseUser;
import com.easy.dfcapi.service.ApiCartService;
import com.easy.dfcapi.service.ApiProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 购物车相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/cart")
public class ApiCartController extends Api20Controller {


    @Autowired
    private ApiCartService cartService;

    /**
     * 购物车增加
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CartAdd", method = RequestMethod.POST)
    public Result CartAdd(Response<CartRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return cartService.CartAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 购物车修改
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CartMod", method = RequestMethod.POST)
    public Result CartMod(Response<CartRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return cartService.CartMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 购物车列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CartList", method = RequestMethod.POST)
    public Result CartList(Response<CartRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return cartService.CartList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 购物车删除
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CartDel", method = RequestMethod.POST)
    public Result CartDel(Response<CartRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return cartService.CartDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
